<?php

namespace Admin\Controller;

/**
 * 试卷控制器
 *
 * @author zhiyuan
 *        
 */
class PapersController extends AdminController {
	public function index() {
		$map ['owner'] = array (
				'eq',
				UID 
		);
		$list = $this->lists ( 'Papers', $map );
		$this->assign ( 'list', $list );
		$this->meta_title = '我的试卷';
		$this->display ();
	}
	public function create() {
		if (IS_POST) {
			$errmsg = '';
			$types_arr = str2arr ( $_POST ['types'], ',' );
			$max_types_count_arr = str2arr ( $_POST ['max_types_count'], ',' );
			$types_count_arr = str2arr ( $_POST ['types_count'], ',' );
			$types_score_arr = str2arr ( $_POST ['types_score'], ',' );
			$types_info_arr = str2arr ( $_POST ['types_info'], '`' );
			
			// 检测试卷总分、套数是否为空
			if ($_POST ['name'] === '') {
				$errmsg = '试卷名称不允许为空';
			}
			if ($errmsg === '' && trim ( $_POST ['mastertitle'] ) === '') {
				$errmsg = '试卷主标题不允许为空';
			}
			if ($errmsg === '' && trim ( $_POST ['secondtitle'] ) === '') {
				$errmsg = '试卷副标题不允许为空';
			}
			
			// 检测试卷总分、套数是否为空
			if ($errmsg === '' && $_POST ['score'] === '') {
				$errmsg = '试卷总分不允许为空';
			}
			if ($errmsg === '' && $_POST ['sets'] === '') {
				$errmsg = '试卷套数不允许为空';
			}
			
			// 检测试卷总分、套数是否为零
			if ($errmsg === '' && $_POST ['score'] === '0') {
				$errmsg = '试卷总分不允许为零';
			}
			if ($errmsg === '' && $_POST ['sets'] === '0') {
				$errmsg = '试卷套数不允许为零';
			}
			
			// 检测试卷总分、套数是否含有前导零
			if ($errmsg === '' && substr ( $_POST ['score'], 0, 1 ) === '0') {
				$errmsg = '试卷总分不允许有前导零';
			}
			if ($errmsg === '' && substr ( $_POST ['sets'], 0, 1 ) === '0') {
				$errmsg = '试卷套数不允许有前导零';
			}
			if ($errmsg === '') {
				foreach ( $types_arr as $key => $value ) {
					// 检测各题型数量、总分及分值分布信息是否为空
					if ($types_count_arr [$key] === '') {
						$errmsg = get_questiontype_name ( $value ) . '数量不允许为空';
						break;
					}
					
					if ($types_score_arr [$key] === '') {
						$errmsg = get_questiontype_name ( $value ) . '总分不允许为空';
						break;
					}
					
					if (trim ( $types_info_arr [$key] ) === '') {
						$errmsg = get_questiontype_name ( $value ) . '分值分布详情不允许为空';
						break;
					}
					
					// 检测各题型数量、总分是否为零
					if ($types_count_arr [$key] === '0') {
						$errmsg = get_questiontype_name ( $value ) . '数量不允许为 零';
						break;
					}
					
					if ($types_score_arr [$key] === '0') {
						$errmsg = get_questiontype_name ( $value ) . '总分不允许为 零';
						break;
					}
					
					// 检测各题型数量、总分是否含有前导零
					if (substr ( $types_count_arr [$key], 0, 1 ) === '0') {
						$errmsg = get_questiontype_name ( $value ) . '数量不允许有前导零';
						break;
					}
					
					if (substr ( $types_score_arr [$key], 0, 1 ) === '0') {
						$errmsg = get_questiontype_name ( $value ) . '总分不允许有前导零';
						break;
					}
					
					// 检测各题型数量设置是否超出可用试题数量上限
					if (( int ) $types_count_arr [$key] > ( int ) $max_types_count_arr [$key]) {
						$errmsg = get_questiontype_name ( $value ) . '数量应在1到' . $max_types_count_arr [$key] . '之间';
						break;
					}
				}
			}
			// 检测各题型设置分数总和与试卷总分是否一致
			if ($errmsg === '') {
				$types_total_score = 0;
				foreach ( $types_score_arr as $key => $value ) {
					$types_total_score += ( int ) $value;
				}
				if ($types_total_score !== ( int ) $_POST ['score']) {
					$errmsg = '各题型设置分数总和与试卷总分不一致';
				}
			}
			/* 检测各章节出题数量是否有效 */
			// 检测章节出题量是否含有前导零以及是否超过数量上限
			if ($errmsg === '') {
				$chapters_type_arr = str2arr ( $_POST ['chapters_type'], ',' );
				$chapters_chapter_arr = str2arr ( $_POST ['chapters_chapter'], ',' );
				$max_chapters_count_arr = str2arr ( $_POST ['max_chapters_count'], ',' );
				$chapters_count_arr = str2arr ( $_POST ['chapters_count'], ',' );
				foreach ( $chapters_count_arr as $key => $value ) {
					if ($value !== '0' && substr ( $value, 0, 1 ) === '0') {
						$errmsg = get_questiontype_name ( $chapters_type_arr [$key] ) . get_chapter_name ( $chapters_chapter_arr [$key] ) . '数量不允许有前导零';
						break;
					}
					if (( int ) $value > ( int ) $max_chapters_count_arr [$key]) {
						$errmsg = get_questiontype_name ( $chapters_type_arr [$key] ) . get_chapter_name ( $chapters_chapter_arr [$key] ) . '超过数量上限' . $max_chapters_count_arr [$key];
						break;
					}
				}
			}
			// 检测章节出题量之和是否超过类型题总量
			if ($errmsg === '') {
				$chapters_type_arr = str2arr ( $_POST ['chapters_type'], ',' );
				$chapters_count_arr = str2arr ( $_POST ['chapters_count'], ',' );
				foreach ( $types_arr as $key => $value ) {
					$tmpsum = 0;
					foreach ( $chapters_type_arr as $key1 => $value1 ) {
						if ($value === $value1) {
							$tmpsum = $tmpsum + ( int ) $chapters_count_arr [$key1];
						}
					}
					if ($tmpsum > ( int ) $types_count_arr [$key]) {
						$errmsg = get_questiontype_name ( $types_arr [$key] ) . '各章节设置数量之和超过该类型题设置的出题数量';
						break;
					}
				}
			}
			// 检测各章节设置用于随机选择的试题数量是否满足该类型题设置的出题数量
			if ($errmsg === '') {
				$chapters_type_arr = str2arr ( $_POST ['chapters_type'], ',' );
				$chapters_count_arr = str2arr ( $_POST ['chapters_count'], ',' );
				foreach ( $types_arr as $key => $value ) {
					$tmpavailablesum = 0;
					$tmpcountsum = 0;
					foreach ( $chapters_type_arr as $key1 => $value1 ) {
						if ($value === $value1) {
							if ($chapters_count_arr [$key1] === '') {
								$tmpavailablesum = $tmpavailablesum + ( int ) $max_chapters_count_arr [$key1];
							} else {
								$tmpcountsum = $tmpcountsum + ( int ) $chapters_count_arr [$key1];
							}
						}
					}
					if (( int ) $types_count_arr [$key] - $tmpcountsum > $tmpavailablesum) {
						$errmsg = get_questiontype_name ( $types_arr [$key] ) . '各章节设置用于随机选择的试题数量不能满足该类型题设置的出题数量';
						break;
					}
				}
			}
			if ($errmsg === '') {
				$papers = D ( 'Papers' );
				$data = $papers->create ();
				if ($data) {
					if ($papers->add ()) {
						$this->success ( '组建成功', U ( 'index' ) );
					} else {
						$this->error ( '组建失败' );
					}
				} else {
					$this->error ( $papers->getError () );
				}
			} else {
				$this->error ( $errmsg );
			}
		} else {
			$ib_id = I ( 'itembank' );
			$ib_info = D ( 'ItemBank' )->field ( true )->find ( $ib_id );
			$map = array (
					'itembank' => array (
							'eq',
							$ib_id 
					),
					'status' => array (
							'eq',
							'1' 
					) 
			);
			$question_types_counts = D ( 'Questions' )->where ( $map )->field ( 'questiontype,count(questiontype) as typecount' )->group ( 'questiontype' )->select ();
			$type_chapter_counts = D ( 'Questions' )->where ( $map )->field ( 'questiontype,chapter,count(chapter) as typechaptercount' )->group ( 'questiontype,chapter' )->select ();
			
			// 类型设置初始值及章节设置
			$types = '';
			$types_count = '';
			$types_score = '';
			$types_info = '';
			foreach ( $question_types_counts as $key => $value ) {
				$types = $types . $question_types_counts [$key] ['questiontype'] . ',';
				$types_count = $types_count . $question_types_counts [$key] ['typecount'] . ',';
				$types_score = $types_score . '10' . ',';
				$types_info = $types_info . '每小题1分' . '`';
				$tmpchaptercount = '';
				foreach ( $type_chapter_counts as $key1 => $value1 ) {
					if ($type_chapter_counts [$key1] ['questiontype'] === $question_types_counts [$key] ['questiontype']) {
						$tmpchaptercount = $tmpchaptercount . $type_chapter_counts [$key1] ['questiontype'] . ':' . $type_chapter_counts [$key1] ['chapter'] . ':' . $type_chapter_counts [$key1] ['typechaptercount'] . '#';
					}
				}
				$tmpchaptercount = substr_replace ( $tmpchaptercount, '', - 1, 1 );
				$question_types_counts [$key] ['typechaptercounts'] = $tmpchaptercount;
			}
			$types = substr_replace ( $types, '', - 1, 1 );
			$types_count = substr_replace ( $types_count, '', - 1, 1 );
			$types_score = substr_replace ( $types_score, '', - 1, 1 );
			$types_info = substr_replace ( $types_info, '', - 1, 1 );
			
			// 章节设置初始值
			$chapters_type = '';
			$chapters_chapter = '';
			$max_chapters_count = '';
			$chapters_count = '';
			foreach ( $type_chapter_counts as $key => $value ) {
				$chapters_type = $chapters_type . $type_chapter_counts [$key] ['questiontype'] . ',';
				$chapters_chapter = $chapters_chapter . $type_chapter_counts [$key] ['chapter'] . ',';
				$max_chapters_count = $max_chapters_count . $type_chapter_counts [$key] ['typechaptercount'] . ',';
				$chapters_count = $chapters_count . ',';
			}
			$chapters_type = substr_replace ( $chapters_type, '', - 1, 1 );
			$chapters_chapter = substr_replace ( $chapters_chapter, '', - 1, 1 );
			$max_chapters_count = substr_replace ( $max_chapters_count, '', - 1, 1 );
			$chapters_count = substr_replace ( $chapters_count, '', - 1, 1 );
			
			$papername = $ib_info ['name'] . '(' . time_format ( NOW_TIME, 'YmdHis' ) . ')';
			$mastertitle = get_semester_name () . '《' . $ib_info ['name'] . '》试卷';
			$secondtitle = '适用专业：' . (( int ) time_format ( NOW_TIME, 'Y' ) - 1) . '级某某专业';
			
			$this->assign ( 'papername', $papername );
			$this->assign ( 'mastertitle', $mastertitle );
			$this->assign ( 'secondtitle', $secondtitle );
			$this->assign ( 'types', $types );
			$this->assign ( 'types_count', $types_count );
			$this->assign ( 'types_score', $types_score );
			$this->assign ( 'types_info', $types_info );
			$this->assign ( 'chapters_type', $chapters_type );
			$this->assign ( 'chapters_chapter', $chapters_chapter );
			$this->assign ( 'max_chapters_count', $max_chapters_count );
			$this->assign ( 'chapters_count', $chapters_count );
			$this->assign ( 'question_types_counts', $question_types_counts );
			$this->assign ( 'ib_info', $ib_info );
			$this->meta_title = '组建试卷';
			$this->display ();
		}
	}
	public function build() {
		// 生成试卷
		$paper_id = I ( 'paper' );
		$paper_info = D ( 'Papers' )->field ( true )->find ( $paper_id );
		if ($paper_info ['owner'] === UID && $paper_info ['status'] === '1') {
			$types_arr = str2arr ( $paper_info ['types'], ',' );
			$types_count_arr = str2arr ( $paper_info ['types_count'], ',' );
			$chapters_type_arr = str2arr ( $paper_info ['chapters_type'], ',' );
			$chapters_chapter_arr = str2arr ( $paper_info ['chapters_chapter'], ',' );
			$chapters_count_arr = str2arr ( $paper_info ['chapters_count'], ',' );
			$itembank = $paper_info ['itembank'];
			$flag = false;
			for($i = 1; $i <= ( int ) $paper_info ['sets']; $i ++) {
				$n = 0;
				$types = '';
				$chapters = '';
				$questions = '';
				foreach ( $types_arr as $key => $value ) {
					$available_chapters = '';
					$remain_count = ( int ) $types_count_arr [$key];
					foreach ( $chapters_type_arr as $key1 => $value1 ) {
						if ($value === $value1) {
							if (( int ) $chapters_count_arr [$key1] > 0) {
								$map = array (
										'itembank' => array (
												'eq',
												$itembank 
										),
										'questiontype' => array (
												'eq',
												( int ) $value 
										),
										'chapter' => array (
												'eq',
												( int ) $chapters_chapter_arr [$key1] 
										),
										'status' => array (
												'eq',
												1 
										) 
								);
								$remain_count = $remain_count - ( int ) $chapters_count_arr [$key1];
								$type_chapter_questions = D ( 'Questions' )->where ( $map )->field ( true )->limit ( ( int ) $chapters_count_arr [$key1] )->order ( 'rand()' )->select ();
								foreach ( $type_chapter_questions as $key2 => $value2 ) {
									$types = $types . $type_chapter_questions [$key2] ['questiontype'] . ',';
									$chapters = $chapters . $type_chapter_questions [$key2] ['chapter'] . ',';
									$questions = $questions . $type_chapter_questions [$key2] ['id'] . ',';
								}
							}
							if ($chapters_count_arr [$key1] === '') {
								$available_chapters = $available_chapters . $chapters_chapter_arr [$key1] . ',';
							}
						}
					}
					$available_chapters = substr_replace ( $available_chapters, '', - 1, 1 );
					if ($remain_count > 0) {
						$map1 = array (
								'itembank' => array (
										'eq',
										$itembank 
								),
								'questiontype' => array (
										'eq',
										( int ) $value 
								),
								'chapter' => array (
										'in',
										$available_chapters 
								),
								'status' => array (
										'eq',
										1 
								) 
						);
						$type_chapter_questions1 = D ( 'Questions' )->where ( $map1 )->field ( true )->limit ( $remain_count )->order ( 'rand()' )->select ();
						foreach ( $type_chapter_questions1 as $key3 => $value3 ) {
							$types = $types . $type_chapter_questions1 [$key3] ['questiontype'] . ',';
							$chapters = $chapters . $type_chapter_questions1 [$key3] ['chapter'] . ',';
							$questions = $questions . $type_chapter_questions1 [$key3] ['id'] . ',';
						}
					}
				}
				
				$types = substr_replace ( $types, '', - 1, 1 );
				$chapters = substr_replace ( $chapters, '', - 1, 1 );
				$questions = substr_replace ( $questions, '', - 1, 1 );
				$paperdetails = D ( 'PaperDetails' );
				$data = $paperdetails->create ();
				$data ['set'] = $i;
				$data ['paper'] = $paper_info ['id'];
				$data ['owner'] = UID;
				// 类型、章节及试题集合
				$data ['types'] = $types;
				$data ['chapters'] = $chapters;
				$data ['questions'] = $questions;
				$data ['create_time'] = NOW_TIME;
				$data ['update_time'] = NOW_TIME;
				$data ['status'] = 1;
				if ($paperdetails->data ( $data )->add ()) {
					$flag = true;
				} else {
					$flag = false;
				}
			}
			if ($flag) {
				$paper = M ( 'Papers' );
				$data = $paper->create ( array (
						'status' => 0 
				) );
				if ($paper->where ( array (
						'id' => ( int ) $paper_id 
				) )->save ( $data )) {
					$flag = true;
				} else {
					$flag = false;
				}
			}
			if ($flag) {
				$this->success ( '组卷成功', U ( 'index' ) );
			} else {
				$this->error ( '组卷失败' );
			}
		} else {
			$this->error ( '对不起，您不具有访问权限!' );
		}
	}
}

?>